var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: null,
    coupon_list: [],
    goods_list: [],
    tabIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function () {
      t.getlist();
    }, 200)
  },
  getlist:function(option){
    var option = { "shop_id": app.globalData.shop_id }
    wx.showLoading({
      title: '加载中…',
    })
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })

    let that = this;

    app.ihttpGet('https://www.58zltc.com/api.php/Shop/item/shop_id/' + option.shop_id + '/openid/' + app.globalData.openid, function (res) {
      that.setData({
        info: res.data
      })
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Goods/allHouDong?shop_id=' + option.shop_id + '&token=' + this.data.token,
      success: res => {
        this.setData({
          goods_list: res.data
        })
      }
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Coupon?shop_id=' + option.shop_id + '&token=' + this.data.token,
      success: res => {
        this.setData({
          coupon_list: res.data
        })
      }
    })
    wx.hideLoading()
  },
  follow: function (e) {
    var info = this.data.info;
    var that = this;
    wx.showLoading({
      title: '加载中…',
    })
    var data = {};
    data.openid = app.globalData.openid;
    data.shop_id = info.shop_id;
    app.ihttpPost('https://www.58zltc.com/api.php/Shop/follow', data, function (res) {
      if (res.data.err_code === 0) {
        if (res.data.err_msg == '-1') {
          info.is_follow = false;
        } else {
          info.is_follow = true;
        }
        that.setData({
          info: info
        })
        wx.hideLoading()
      } else {
        wx.showToast({
          title: res.data.err_msg
        })
      }
    })
  },
  bindCall: function (e) {
    console.log(e);
    var tel = e.currentTarget.dataset.tel;
    console.log(tel);
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },
  openLocation: function (e) {
    var data = e.currentTarget.dataset;
    wx.openLocation({
      latitude: Number(data.latitude),
      longitude: Number(data.longitude),
      name: data.name,
      address: data.address
    })
  },
  setTab: function (e) {
    console.log(e);
    this.setData({
      tabIndex: e.currentTarget.dataset.id
    })
  },
  getCoupon: function (e) {
    let that = this;
    const id = e.currentTarget.dataset.id;
    var post_data = {};
    post_data.coupon_id = id;
    post_data.openid = app.globalData.openid;
    app.ihttpPost("https://www.58zltc.com/api.php/Coupon/getCoupon", post_data, function (res) {
      app.iModel(res.data.err_msg);
    })
  },
  renling: function (e) {
    wx.navigateTo({
      url: 'renling?shop_id=' + this.data.info.shop_id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const shop = this.data.info;
    return {
      title: shop.name,
      path: '/pages/shop/detail?shop_id=' + shop.shop_id
    }
  },
  previewImg: function (e) {
    console.log(e);
    var url = e.target.dataset.url;
    wx.previewImage({
      current: url,
      urls: [url]
    })
  }
})